package tester.main;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import state.Globals;

/**
 * This class is the main entry point for the program
 * @author Caleb Chan
 *
 */
public class DispatcherMain {
	
	// Flag used for debug mode settings
	private static boolean DEBUG_MODE = true;
	
	/**
	 * This method will print out the info required to run this code
	 */
	public static void printInfo(){
		System.out.println("Usage: ");
		System.out.println("Java -jar DispatcherMain.jar <test config xml file>");
	}

	/**
	 * The main method
	 * @param args The arguments
	 */
	public static void main(String args[]){
		
		// Checks if the amount of arguments is correct
		// Will only check in non-debug mode runs
		if (!DispatcherMain.DEBUG_MODE){
			if (args.length != 1){
				DispatcherMain.printInfo();
				System.exit(0);
			}
		}
		
		int port = 0;
		
		try {
			// Inits the logger
			try {
				Globals.initLogger("Tester");
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			// Creates the testing framework
			DispatcherTester tester = new DispatcherTester(InetAddress.getLocalHost(), port);
			
			// Parses the XML file
			if (DispatcherMain.DEBUG_MODE){
				tester.parseFile("test_meta_data.xml");
			}else{
				tester.parseFile(args[0]);
			}
			
			// Inits the Group and Person Handlers
			tester.initializeGroupHandler();
			tester.initializePersonHandler();
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// Runs the test
			tester.runTestSuite();
			
			tester.shutdownHandlers();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}
